package com.anan.service.impl;

import com.anan.constants.Constants;
import com.anan.mapper.ImgResourceMapper;
import com.anan.mapper.RemarkMapper;
import com.anan.mapper.StudentMapper;
import com.anan.pojo.bo.RemarkBO;
import com.anan.pojo.domain.ImgResourceDO;
import com.anan.pojo.domain.RemarkDO;
import com.anan.pojo.domain.StudentDO;
import com.anan.pojo.dto.AddRemarkDTO;
import com.anan.pojo.dto.QryRemarkDTO;
import com.anan.pojo.vo.RemarkOfOpusAndProdVO;
import com.anan.service.RemarkService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author anan
 * @date 2023/05/10/12:08
 **/
@Service
public class RemarkServiceImpl implements RemarkService {
    @Resource
    private RemarkMapper remarkMapper;
    @Resource
    private ImgResourceMapper imgResourceMapper;
    @Resource
    private StudentMapper studentMapper;

    @Override
    public boolean addOneRemark(AddRemarkDTO addRemarkDTO) {
        if (null != addRemarkDTO) {
            RemarkDO remarkDO = new RemarkDO();
            remarkDO.setStudentId(addRemarkDTO.getStudentId());
            remarkDO.setRemark(addRemarkDTO.getRemark());
            if (Constants.TYPE.PRODUCT == addRemarkDTO.getQryType()) {
                remarkDO.setRemarkType(Constants.TYPE.PRODUCT);
                remarkDO.setRelateId(addRemarkDTO.getProductId());
            }
            if (Constants.TYPE.OPUS == addRemarkDTO.getQryType()) {
                remarkDO.setRemarkType(Constants.TYPE.OPUS);
                remarkDO.setRelateId(addRemarkDTO.getOpusId());
            }
            return remarkMapper.addRemark(remarkDO);
        }
        return false;
    }

    @Override
    public RemarkOfOpusAndProdVO getRemarkOfOpus(QryRemarkDTO qryRemarkDTO) {
        if (qryRemarkDTO != null) {
            RemarkOfOpusAndProdVO remarkOfOpusVO = new RemarkOfOpusAndProdVO();
            List<RemarkDO> remarkList = new ArrayList<>();
            if (Constants.TYPE.PRODUCT == qryRemarkDTO.getQryType()) {
                 remarkList = remarkMapper.getRemarkByRelateIdIdAndStudentId(qryRemarkDTO.getProductId(), qryRemarkDTO.getStudentId(), Constants.TYPE.PRODUCT);
            }
            if (Constants.TYPE.OPUS == qryRemarkDTO.getQryType()) {
                remarkList = remarkMapper.getRemarkByRelateIdIdAndStudentId(qryRemarkDTO.getOpusId(), qryRemarkDTO.getStudentId(), Constants.TYPE.OPUS);
            }
            List<RemarkBO> remarks = new ArrayList<>();
            remarkList.forEach(remarkDO -> {
                RemarkBO remarkBO = new RemarkBO();
                BeanUtils.copyProperties(remarkDO, remarkBO);
                //查询头像
                ImgResourceDO imgResourceDO = imgResourceMapper.getHeadUrlByStudentId(Constants.TYPE.STUDENT, remarkDO.getStudentId());
                if (null != imgResourceDO) remarkBO.setHeadUrl(imgResourceDO.getImgUrl());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                remarkBO.setCreateDate(simpleDateFormat.format(remarkDO.getCreateDate()));

                StudentDO studentDO = studentMapper.getStudentById(remarkDO.getStudentId());
                if(null != studentDO && null != studentDO.getStudentId()) remarkBO.setNickName(studentDO.getNickname());
                remarks.add(remarkBO);
            });

            remarkOfOpusVO.setRemarkList(remarks);
            return remarkOfOpusVO;
        }
        return null;
    }
}
